package com.proactive.webapp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.CourseCategory1DAO;
import com.proactive.webapp.dao.CourseCategory2DAO;
import com.proactive.webapp.dao.CourseCommentDAO;
import com.proactive.webapp.dao.CourseDAO;
import com.proactive.webapp.dao.CourseTagDAO;
import com.proactive.webapp.dao.DesignDAO;
import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.Course;
import com.proactive.webapp.entity.CourseCategory1;
import com.proactive.webapp.entity.CourseCategory2;
import com.proactive.webapp.entity.CourseComment;
import com.proactive.webapp.entity.CourseTag;
import com.proactive.webapp.entity.Design;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.util.Category;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;
@Controller
public class CourseController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/course", method = RequestMethod.GET)
	public ModelAndView getCourseList(HttpServletRequest request, HttpSession session) {
		//import DAO
		CourseCategory1DAO courseCategory1DAO = (CourseCategory1DAO) context.getBean("courseCategory1DAO");
		CourseCategory2DAO courseCategory2DAO = (CourseCategory2DAO) context.getBean("courseCategory2DAO");
		CourseDAO courseDAO = (CourseDAO) context.getBean("courseDAO");
		ModelAndView model = new ModelAndView();
		
		
		//get data from request
		String type = request.getParameter("t")==null?"new":request.getParameter("t");
		String category1Req = request.getParameter("c1")==null?"":request.getParameter("c1");
		String category2Req = request.getParameter("c2")==null?"":request.getParameter("c2");
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		
		//define id
		int category1ID = category1Req.equals("")?0:Integer.parseInt(category1Req);
		int category2ID = category2Req.equals("")?0:Integer.parseInt(category2Req);
		
		//set pager
		int pageSize = 16;
		int count = courseDAO.countTotal(category1ID, category2ID);
		String pageUrl = "course";
		String pageEndUrl1 = "&t="+type+"&c1="+category1ID+"&c2="+category2ID;
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
		pageCount.calculate();
		
		//set category
		String pageEndUrl2 = "&c1="+category1ID+"&c2="+category2ID;
		Category category = new Category(type, category1ID, category2ID, pageUrl, pageEndUrl2);
		category.set1();
		
		//get course by condition
		List<Course> courseList = new ArrayList<Course>();
		List<CourseCategory1> category1List = new ArrayList<CourseCategory1>();
		List<CourseCategory2> category2List = new ArrayList<CourseCategory2>();
		category1List = courseCategory1DAO.getList();
		category2List = courseCategory2DAO.getList(category1ID);
		if(category1ID != 0 && category2ID == 0)
			courseList = courseDAO.getListByCategory1(type, category1ID, pageCount.getStart(), pageSize);
		else if(category2ID != 0)
			courseList = courseDAO.getListByCategory2(type, category2ID, pageCount.getStart(), pageSize);
		else
			courseList = courseDAO.getListByType(type, pageCount.getStart(), pageSize);

		//set category1 list(class,url,name...)
		CourseCategory1 category1Index = new CourseCategory1();
		category1Index.setId(0);
		category1Index.setName("全部分類");
		category1Index.setClassString(category1ID==0?"selected":"");
		category1Index.setUrl(pageUrl+"?t="+category.getType());
		category1List.add(0, category1Index);
		for(int i=0; i<category1List.size(); i++){
			CourseCategory1 category1 = category1List.get(i);
			if(category1.getId() == category1ID){
				category.setName1(category1.getName());
				category1List.get(i).setClassString("selected");
			}
			String url = pageUrl+"?t="+category.getType()+"&c1="+category1.getId();
			category1List.get(i).setUrl(url);
				
		}
		
		//set category2 list(class,url,name...)
		CourseCategory2 category2Index = new CourseCategory2();
		category2Index.setId(0);
		category2Index.setName("全部分類");
		category2Index.setClassString(category2ID==0?"selected":"");
		category2Index.setUrl(pageUrl+"?t="+category.getType()+"&c1="+category1ID);
		category2List.add(0, category2Index);
		for(int i=0; i<category2List.size(); i++){
			CourseCategory2 category2 = category2List.get(i);
			if(category2.getId() == category2ID){
				category.setName2(category2.getName());
				category2List.get(i).setClassString("selected");
			}
			String url = pageUrl+"?t="+category.getType()+"&c1="+category1ID+"&c2="+category2.getId();
			category2List.get(i).setUrl(url);
			
		}

		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 1);
		
		//set model
		model.addObject("page", pageCount);
		model.addObject("courseList", courseList);
		model.addObject("category1", category1List);
		model.addObject("category2", category2List);
		model.addObject("category", category);
		model.addObject("menuList", menuList);
		

		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		model.setViewName(loginMember!=null?"course1":"course2");
		
		return model;
	}
	
	@RequestMapping(value = "/showCourse", method = RequestMethod.GET)
	public ModelAndView getCourse(HttpServletRequest request, HttpSession session) {
		CourseDAO courseDAO = (CourseDAO) context.getBean("courseDAO");
		MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
		CourseTagDAO courseTagDAO = (CourseTagDAO) context.getBean("courseTagDAO");
		CourseCommentDAO courseCommentDAO = (CourseCommentDAO) context.getBean("courseCommentDAO");
		DesignDAO designDAO = (DesignDAO) context.getBean("designDAO");
		ModelAndView model = new ModelAndView();
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")){
			//check login
			Member loginMember = (Member)session.getAttribute("loginMember");
			
			int courseID = Integer.parseInt(request.getParameter("id"));
			int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
			
			Course course = new Course();
			course.setId(courseID);
			Member member = new Member();
			List<CourseComment> commentList = new ArrayList<CourseComment>(); 
			List<CourseTag> tagList = new ArrayList<CourseTag>();
			List<Course> courseTop3List = new ArrayList<Course>();
			List<Design> designList = new ArrayList<Design>();

			designList = designDAO.getPracticeListByTop(courseID, 6);
			
			//set pager
			int pageSize = 5;
			int countComment = courseCommentDAO.countTotal(course.getId());
			String pageUrl = "showCourse";
			String pageEndUrl1 = "&id="+course.getId()+"#commentArea";
			PageCount pageCount = new PageCount(pageNow, pageSize, countComment, pageUrl, pageEndUrl1);
			pageCount.calculate();
			
			
			String loginAccount = loginMember!=null?loginMember.getAccount():"";
			if(loginMember != null){
				course = courseDAO.getByLogin(course, loginMember.getAccount());
				member.setAccount(course.getAccount());
				member = memberDAO.getByLogin(member, loginMember.getAccount());
				if(course.getStatus()!=1){
					if(course.getStatus() != 3)
						model.setViewName(course.getAccount().equals(loginAccount)?"courseDetail1":"redirect:/course");
					else
						model.setViewName("redirect:/course");
				}
				else
					model.setViewName("courseDetail1");
			}
			else{
				course = courseDAO.get(course);
				member.setAccount(course.getAccount());
				member = memberDAO.get(member);
				model.setViewName(course.getStatus()==1?"courseDetail2":"redirect:/course");
			}
			course.setViewCount(course.getViewCount()+1);
			courseDAO.updateViewCount(course);
			commentList = courseCommentDAO.getList(loginAccount, courseID, pageCount.getStart(), pageSize);
			tagList = courseTagDAO.getListByCourseID(courseID);
			courseTop3List = courseDAO.getListByCategory2Top3(course.getCategory2ID(), course.getId());

			int count = courseDAO.countTotal(0, course.getCategory2ID());
			course.setMoreFlag((courseTop3List.size()==2||count>3)?true:false);
			course.setMoreString(course.isMoreFlag()?"更多":"沒有更多");
			course.setMoreUrl(course.isMoreFlag()?course.getCategoryUrl():"javascript:;");
			for(int i=0; i<commentList.size(); i++){
				CourseComment courseComment = commentList.get(i);
				List<CourseComment> replyCommentList = courseCommentDAO.getReplyList(loginAccount, courseComment.getId());
				commentList.get(i).setReplyCommentList(replyCommentList);
				if(courseComment.getAccount().equals(loginAccount))
					commentList.get(i).setDeleteFlag(true);
				
				for(int j=0; j<replyCommentList.size(); j++){
					CourseComment replyComment = replyCommentList.get(j);
					if(replyComment.getAccount().equals(loginAccount))
						commentList.get(i).getReplyCommentList().get(j).setDeleteFlag(true);
				}
			}
			
			if(courseTop3List.size() == 2)
				courseTop3List.remove(1);
			for(int i=0; i<courseTop3List.size(); i++){
				courseTop3List.get(i).setClassString(i==1?"modify2":"modify");					
			}
			
			course.setContent(StringEscapeUtils.unescapeHtml(course.getContent()));
			
			List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 1);
			
			model.addObject("course", course);
			model.addObject("member", member);
			model.addObject("commentList", commentList);
			model.addObject("courseList", courseTop3List);
			model.addObject("design", designList);
			model.addObject("tagList", tagList);
			model.addObject("page", pageCount);
			model.addObject("menuList", menuList);
		}
		else
			model.setViewName("redirect:/course");
		return model;
	}
	
	
}
